//
//  SHStatusViewModel.swift
//  SHWeibo01
//
//  Created by 卫平玉 on 16/7/23.
//  Copyright © 2016年 卫平玉. All rights reserved.
//

import UIKit

class SHStatusViewModel: NSObject {

    // 定义一个属性
    var status: SHStatus?
    // 微博认证用户图片
    var avatarImage: UIImage?
    //处理会员等级图片
    var memberImage: UIImage?
    //微博处理富文本
    var sourceAttr: NSMutableAttributedString?
    // 转发数
    var repostsCountStr: String?
    // 评论数
    var commentsCountStr: String?
    // 表态数
    var attitudesCountStr: String?
    
    var sinaTimeStr: String?{
        
        return NSDate.sinaDate(status?.created_at ?? "")?.sinaDescripitionStr
    }
    
    
    
    init(status:SHStatus) {
        
        super.init()
        self.status = status
        
        self.avatarImage = getAvatarImage(status.user?.verified)
        
        self.memberImage = getMemberImage(status.user?.mbrank)
        
        self.sourceAttr = getSourceStr(status.source)
        
        self.repostsCountStr = getSinaCount(status.reposts_count, title: "转发")
        self.commentsCountStr = getSinaCount(status.comments_count, title: "评论")
        self.attitudesCountStr = getSinaCount(status.attitudes_count, title: "赞")
        
    }
    
  
}

extension SHStatusViewModel{
    // 处理微博认证用户图片/// 认证类型 -1：没有认证，1，认证用户，2,3,5: 企业认证，220: 达人
    func getAvatarImage(verified: Int?) -> UIImage?{
        
        guard let ver = verified else{
           
            return nil
        }
        
        switch ver {
            
        case 1:
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
            
        case 220:
            return UIImage(named: "avatar_grassroot")
            
        default:
            return UIImage(named: "avatar_vgirl")
        }
    }
    
    func getMemberImage(mbrank: Int?) -> UIImage?{
        
        guard let mb = mbrank else{
            
            return nil
        }
        
        if mb > 0 && mb < 7 {
            
            return UIImage(named: "common_icon_membership_level\(mb)")
        }else{
            return UIImage(named: "common_icon_membership")
        }
    }
    
}

// MARK: - 出来微博，来自业务逻辑处理
extension SHStatusViewModel{
    
    func getSourceStr(source: String?)-> NSMutableAttributedString{
        //判断一个字符串是否包含某个字符，用containsString
        guard let s = source where s.containsString("\">") else{
            
            let str = "上海三期微博"
            return getSourceAttr(str)
        }
        //第一个的最后一个到最后一个的第一个
        let startRange = s.rangeOfString("\">")!
        let endRange = s.rangeOfString("</")!
        
        let result = s.substringWithRange(startRange.endIndex..<endRange.startIndex)
        
        return getSourceAttr(result)
    }
    // 处理显示微博来源的富文本
    func getSourceAttr(result: String) -> NSMutableAttributedString{
        
        let name = "来自 " + result
        //得到range
        let range = (name as NSString).rangeOfString("来自")
         // 创建一个可变富文本
        let  attr = NSMutableAttributedString(string: name)
        //设置属性
        attr.addAttributes([NSForegroundColorAttributeName:UIColor.grayColor()], range: range)
        
        return attr
        
    }
}

// MARK: - 处理转发评论赞
/*
- 转发 评论 赞 业务逻辑处理
- 如果count <= 0  显示 转发 评论 赞 例如 0  显示 转发 评论 赞
- 如果count > 0
- 如果 count < 10000  数据是多少显示多少  例如  8888  显示 8888
- 如果 count >= 10000  显示 x.x万       例如 12000  显示 1.2 万
- 注意 如果 count%10000 = 0  显示  例如 20000 显示 2万
*/
extension SHStatusViewModel{
    
    func getSinaCount(count: Int,title: String) -> String{
        
        if count <= 0{
            
            return title
        }else{
            
            if count < 10000{
                
                return "\(count)"
            }else{
                
                let sinaCount = CGFloat(count/1000)/10
                
                var sinaCountStr = "\(sinaCount)万"
                
                if sinaCountStr.containsString(".0"){
                    
                    sinaCountStr = sinaCountStr.stringByReplacingOccurrencesOfString(".0", withString:"")
                }
                
                return sinaCountStr
            }
        }
    }
}
